<?php
//$goperdb = new dboper(db_type,db_database,db_user,db_pass,db_host);

class dboper 
{
	var $db;
	var $sql;
	var $rs;
	var $err;
	var $tmp1;
	var $tmp2;
	var $tmp3;
	var $tmp4;
	var $cnt;
	var $fld;
	var $typ;
	var $drs;

	function dboper($dtip,$ddat,$duse,$dpas,$dhost) 
	{
		$this->db = &ADONewConnection($dtip);
		$this->db->Connect($dhost,$duse,$dpas,$ddat);
//		$this->sql = "ALTER SESSION SET NLS_DATE_FORMAT = 'DD/MM/YYYY'";
//		$this->db->Execute($this->sql);
//		$this->sql = "SET TRANSACTION USE ROLLBACK SEGMENT CENTRALIZZAZIONE_RB01";
//		$this->db->Execute($this->sql);
	}

	function auth_user($user,$pass,&$uid,&$group) {
		$this->sql= "select a.ute_uid,b.ute_nome from ute_utenti a, ute_gruppi b ";
		$this->sql.="where a.ute_alias='$user' and a.ute_pass='$pass'";
		$this->sql.="  and b.ute_id=a.ute_gruppo";
		if ($this->rs) $this->rs->Close();
		$this->rs = $this->db->Execute($this->sql);
		if (!$this->rs) {
        	print $this->db->ErrorMsg();
			$udi = 0;
			$group = 0;
		}
		if ($this->rs->EOF) {
			$uid = -1;
			$group = -1;
		} else {
			$uid = $this->rs->fields[0];
			$group = $this->rs->fields[1];
		}
	}

	function ExecSql($sSql) {
		$this->err = "";
		if ($this->rs) $this->rs->Close();
		$this->rs = $this->db->Execute($sSql);
		if (!$this->rs) {
//				$this->err = $this->db->ErrorMsg();
				return "ERR";
		}
		if ($this->rs->EOF) {
			return "VUOTO";
		} else {
			return "OK";
		}
	}

	function FieldType(&$ptype,&$pkey) {
		if (preg_match("/num_/i",$pkey)) {
			$pkey=preg_replace("/num_/","",$pkey);
			$ptype="numeric";
		} elseif (preg_match("/dat_/i",$pkey)) {
			$pkey=preg_replace("/dat_/","",$pkey);
			$ptype="date";
		} elseif (preg_match("/txt_/i",$pkey)) {
			$pkey=preg_replace("/txt_/","",$pkey);
			$ptype="string";
		} elseif (preg_match("/val_/i",$pkey)) {
			$pkey=preg_replace("/val_/","",$pkey);
			$ptype="curency";
		} elseif (preg_match("/cur_/i",$pkey)) {
			$pkey=preg_replace("/cur_/","",$pkey);
			$ptype="curency";
		} elseif (preg_match("/bol_/i",$pkey)) {
			$pkey=preg_replace("/bol_/","",$pkey);
			$ptype="boolean";
		} else {
			$ptype="none";
		}	
	}

	function Format(&$ptype,&$pval) {
		switch($ptype) {
			case "numeric":
                if ($pval=="")
                    return '0';
                else 
				    return $pval;
				break;
			case "date":
				return "STR_TO_DATE('".$pval."',GET_FORMAT(DATE,'EUR'))";
				break;
			case "curency":
				if ($pval != "") {
					$pval = str_replace(".","",$pval);
					return str_replace(",",".",$pval);
				} else {
					return "0";
				}
				break;
			case "boolean":
				return ($pval=="on")? "'S'" : "'N'" ;
				break;
			default:
				return "'".str_replace("'", "''", $pval)."'";
		}
	}

	function ViewFormat($field,$type) {
		foreach($this->drs as $this->kval => $this->val) {
			if ($this->kval==$field) {
				switch($type) {
					case "n":
						return $this->val;
						break;
					case "d":
						return $this->val;
						break;
					case "c":
						return $this->val;
						break;
					default:
						return $this->val;
				}
			}
		}
	}

/*
	$dati = recordset della query per la visualizzazione del record
	$post = array POST o altro array da utlizzare per conteneri i valori da inserire
			nel value
	$ass  = questo fornisce un eventuale collegamento tra i nomi dei campi e i nomi
			utilizzati nel form html o dagli oggetti
	$drs  = un array interno che permetti di utilizzare la funzione ViewFormat per recuperare
			i valori associanti a una campo e formattarli
*/
	function FormView(&$dati,&$post,&$ass) {
		$this->cnt=0;
		$this->tmp1="";
		$drs=array();
		if (count($dati->fields) > 0) {
			foreach($dati->fields as $this->kval => $this->val) {
				if ($this->tmp1!="") {
	//				$this->fld = $dati->FetchField($this->cnt);
	//				$this->typ = $dati->MetaType('','',$this->fld->type);
					if (isset($ass[$this->kval])) {
						$post[$ass[$this->kval]]=$this->val;
						$drs[$ass[$this->kval]]=$this->val;
					} else {
						$post[$this->kval]=$this->val;
						$drs[$this->kval]=$this->val;
					}
					$this->tmp1="";
	//print $this->cnt."\t\t".$this->fld->type." ".$this->kval."=".$post[$this->kval]."<br>\n";
				} else {
					$this->tmp1="ok";
				}
				$this->cnt++;
			}		
		}
	}

//	function FormSql(&$dati,$prefix,$type,$table,&$ext=array(),$esc="",&$whe=array()) {
	function FormSql(&$dati,$prefix,$type,$table,&$ext,$esc,&$whe) {
		$this->tmp1="";
		$this->tmp2="";
		$this->tmp3="";
		$this->tmp4="";
		switch (strtoupper($type)) {
			case "INSERT" :
				if ($esc!="ALL") {
					foreach($dati as $this->kval => $this->val) {
						if (!preg_match("/".$this->kval."/i",$esc)) {
							if (preg_match("/".$prefix."/i",$this->kval)) {
								if ($tmp3!="") {
									$tmp1.=",";
									$tmp2.=",";
								}
								$this->FieldType($tmp4,$this->kval);
								$tmp2.=$this->Format($tmp4, $this->val);
								$tmp1.=$this->kval;
								$tmp3="ok";
							}
						}
					}
				}
				foreach($ext as $this->kval => $this->val) {
					if ($tmp3!="") {
						$tmp1.=",";
						$tmp2.=",";
					}
					$this->FieldType($tmp4,$this->kval);
					$tmp1.=$this->kval;
					$tmp2.=$this->Format($tmp4, $this->val);
				}
				return "INSERT INTO ".$table." (".$tmp1.") values (".$tmp2.")";
				break;
//--------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------
			case "UPDATE" :
				if ($esc!="ALL") {
					foreach($dati as $this->kval => $this->val) {
						if (!preg_match("/".$this->kval."/i",$esc)) {
							if (preg_match("/".$prefix."/i",$this->kval)) {
								if ($tmp3!="") $tmp1.=",";
								$this->FieldType($tmp4,$this->kval);
								$tmp1.=$this->kval."=";
								$tmp1.=$this->Format($tmp4, $this->val);
								$tmp3="ok";
							}
						}
					}
				}
				foreach($ext as $this->kval => $this->val) {
					if ($tmp3!="") $tmp1.=",";
					$this->FieldType($tmp4,$this->kval);
					$tmp1.=$this->kval."=";
					$tmp1.=$this->Format($tmp4, $this->val);
					$tmp3="ok";
				}
				$tmp3="";
				$tmp2="";
				foreach($whe as $this->kval => $this->val) {
					if ($tmp3!="") $tmp2.=" AND ";
					$this->FieldType($tmp4,$this->kval);
					$tmp2.=$this->kval."=";
					$tmp2.=$this->Format($tmp4, $this->val);
					$tmp3="ok";
				}
				return "UPDATE ".$table." SET ".$tmp1." WHERE ".$tmp2;
				break;
		}
	}
}
?>